﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.DataAnnotations;

namespace CommunitySystem.Model.Silverlight
{
    public class NumberValidation : ValidationAttribute
    {
        private int _MinValue = 0;

        public int MinValue
        {
            get { return _MinValue; }
            set { _MinValue = value; }
        }

        private int _MaxValue = 0;

        public int MaxValue
        {
            get { return _MaxValue; }
            set { _MaxValue = value; }
        }

        public NumberValidation()
        {
            
        }

        public NumberValidation(int Min, int Max)
        {
            _MinValue = Min;
            _MaxValue = Max;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            try
            {
                int num = (int)value;
                if (_MaxValue != 0 || _MinValue != 0)
                {
                    if (num > _MaxValue || num < _MinValue)
                        return new ValidationResult("输入值必须介于" + _MinValue + "和" + _MaxValue + "之间");
                }
            }
            catch (Exception ex)
            {
                return new ValidationResult("必须输入数字");
            }
            return ValidationResult.Success;

        }
        
    }
}
